//
//  TxtFileController.m
//  gdemm
//
//  Created by 乔明超 on 16/10/9.
//  Copyright © 2016年 Goldencis. All rights reserved.
//

#import "TxtFileController.h"
#import "UserDefaultsUtil.h"
@interface TxtFileController ()<UIGestureRecognizerDelegate,UITextViewDelegate>

@end

@implementation TxtFileController

- (UITextView *)txtView{
    if (_txtView == nil) {
        _txtView = [[UITextView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight)];
        _txtView.backgroundColor = [UIColor clearColor];
        [_txtView setEditable:YES];
        [_txtView setDelegate:self];
        
        UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:nil];
        
        longPress.delegate = self; //记得在.h文件里加上委托
        
        longPress.minimumPressDuration = 0.4;

        [_txtView addGestureRecognizer:longPress];
        [self.view addSubview:_txtView];
    }
    return _txtView;
}
-(BOOL)textViewShouldBeginEditing:(UITextView *)textView
{
    return NO;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
    if ([DeviceTypeJudge isPadDeviceType]) {
        button.frame = CGRectMake(0, 0, 20, 20);
    }else{
        button.frame = CGRectMake(0, 0, 16, 16);
    }
    
    [button setImage:[[UIImage imageNamed:@"navigationBar_back"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(leftButtonPopHandler) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *leftButton = [[UIBarButtonItem alloc]initWithCustomView:button];
    self.navigationItem.leftBarButtonItem = leftButton;
    self.navigationItem.title = NSLocalizedString(@"str_cloud_disk",  @"云盘");
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    //判断是否打开了水印策略，添加水印效果
    if ([[UserDefaultsUtil shareInstance] getBool:LC_OPENWATERMARKPOLICY]) {
        for (int i = 0; i < 3; i++) {
            UILabel * label = [[UILabel alloc]initWithFrame:CGRectMake(20, i * (WINDOWHEIGHT - 200)/2, 250, 80)];
            label.font = [UIFont systemFontOfSize:16];
            label.textColor = [UIColor grayColor];
            label.transform = CGAffineTransformMakeRotation(-M_1_PI);
            label.text =  [[UserDefaultsUtil shareInstance]getString:LC_WATRMARKTEXT];
            [self.view addSubview:label];
        }
    }
    [self showFile];
}
-(void)leftButtonPopHandler{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void) showFile
{
    if (self.path)
    {
        NSURL* url = [NSURL fileURLWithPath:self.path];
        if (url != nil)
        {
            NSString* content = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil];
            if (content == nil)
            {
                NSStringEncoding gbkEncoding = CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingGB_18030_2000);
                content = [NSString stringWithContentsOfURL:url encoding:gbkEncoding error:nil];
            }
            
            if (content != nil)
            {
                [self.txtView setText:content];
            }
        }
    }
}

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer

{
    return NO;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
@end
